// 获取某一个url参数的值
export const getUrlParam = function (key) {
    const search = location.search.substr(1);
    const param = search.match(new RegExp(`(^|&)${key}=([^&]*)(&|$)`))
    return param ? param[2] : null
}

// 判断是否存在url参数的值
export const hasUrlParam = function (key) {
    const search = location.search.substr(1);
    const param = search.match(new RegExp(`(^|&)${key}=([^&]*)(&|$)`))
    return !!param
}

// 获取所有url参数的值
export const getUrlParams = url => {
    const reg = /([^=&\s]+)[=\s]*([^&\s]*)/g;
    const search = url || location.search.substr(1);
    const params = {};
    while (reg.exec(search)) params[RegExp.$1] = decodeURIComponent(RegExp.$2);
    return params;
}

/**
 * 时间格式yyyy-MM-dd HH:mm:ss
 */
export function dateFormatter(date) {
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const strDate = date.getDate().toString().padStart(2, '0');
    return `${date.getFullYear()}-${month}-${strDate} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
}

/**
 * 时间格式yyyy-MM-dd_HH-mm-ss
 */
export function dateFormatter2(date) {
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const strDate = date.getDate().toString().padStart(2, '0');
    return `${date.getFullYear()}-${month}-${strDate}_${date.getHours()}-${date.getMinutes()}-${date.getSeconds()}`;
}
